<?php


if(!defined('DIR_VSCORE'))
	define('DIR_VSCORE', DIR_LIB.'VsCore'.DIR_SEP);

if(!defined('DIR_JUNONA'))
	define('DIR_JUNONA', DIR_LIB.'Junona'.DIR_SEP);


require_once(DIR_VSCORE."db.php");

require_once(DIR_LIB."VsGui".DIR_SEP."vspager.php");

require_once(DIR_JUNONA."junmodel.php");
require_once(DIR_JUNONA."junmodeloperation.php");
require_once(DIR_JUNONA."junoperator.php");

class JunObjectsFactory {
	private static $_settings = null;
	
	static function makeSettings() {
		$auth = VsAuth::Instance();
		
		if(self::$_settings === null) {
			self::$_settings = array();
			$q = "SELECT * FROM `settings`";
		
			$db = VsDatabase::Instance();
			$res = $db->query($q, false);
			while($rs = mysql_fetch_object($res)) {
				self::$_settings[$rs->varName] = $rs->varValue;
			}
		}
	
		return self::$_settings;
	}
	//================================================================================
	
	/**
	 * 
	 * @param array $settings Key/Value Pairs
	 * @return int
	 */
	static function saveSettings($settings) {
		//die(var_dump($settings));
		$auth = VsAuth::Instance();
		$db = VsDatabase::Instance();
		
		$ret = 0;
		
		foreach($settings as $k=>$v) {
			$q = "UPDATE `settings`";
			$q .= " SET varValue='".$v."' WHERE varName='".$k."'";
			//die(var_dump($q));
			$res = $db->query($q);
			
			// Count rows changed
			if($res !== null) {
				$ret += $res;
			}
		}
		
		return $ret;
	}
	//================================================================================
	
	static function countModels()
	{
		$db = VsDatabase::Instance();
		
		$qCount = "SELECT COUNT(*) FROM `".JunModel::$_sqlTable."` WHERE removed=0";
		$resCount = $db->query($qCount);
		$row = mysql_fetch_array($resCount);
		
		return is_array($row) ? $row[0] : 0;
	}
	
	static function makeModels($pager=null) {
		$auth = VsAuth::Instance();
		$db = VsDatabase::Instance();
		
		$listModels = array();
		$q = "SELECT * FROM `".JunModel::$_sqlTable."` WHERE removed=0";
		if($auth->id()!=3)
			$q .= " AND `added_by`!=3";

		$q .= " ORDER BY CAST(number AS UNSIGNED) DESC";	//ORDER BY `number`
		
		// Add Paging
		if($pager) {
			$q .= sprintf(" LIMIT %d , %d", $pager->from(), $pager->view());
		}
		
		$res = $db->query($q);
		while($rm = mysql_fetch_assoc($res)) {
			if($model = JunModel::makeFromDb($rm))
				$listModels[] = $model;
		}

		return $listModels;
	}
	//================================================================================

	static function makeModel($modelId)  {
		$auth = VsAuth::Instance();
		$q = "SELECT * FROM `".JunModel::$_sqlTable."` WHERE id=".$modelId;
		if($auth->id()!=3)
			$q .= " AND `added_by`!=3";

		$db = VsDatabase::Instance();
		$res = $db->query($q);
		if($rm = mysql_fetch_assoc($res)) {
			$model = JunModel::makeFromDb($rm);
		}
		else
			$model = JunModel::makeNew();
 
		return $model;
	}
	//================================================================================

	static function makeOperations($modelId) {
		$auth = VsAuth::Instance();
		$listOperations = array();
		$q = "SELECT * FROM `".JunOperation::$_sqlTable."` WHERE model_id=".$modelId." AND removed=0";
		if($auth->id()!=3)
			$q .= " AND `added_by`!=3";

		$q .= " ORDER BY CAST(operation_id AS UNSIGNED)";		// ORDER BY `operation_id`

		$db = VsDatabase::Instance();
		$res = $db->query($q);

		$total = 0;
		while($ro = mysql_fetch_assoc($res)) {
			if($operation = JunOperation::makeFromDb($ro)) {
				$listOperations[] = $operation;

				$totalMinutes += $operation->minutes();
				$totalPrice += $operation->price();
			}
		}

		return array($listOperations, $totalPrice, $totalMinutes);
	}
	//================================================================================

	static function makeOperation($id) {
		$auth = VsAuth::Instance();
		$q = "SELECT * FROM `".JunOperation::$_sqlTable."` WHERE id=".$id;
		if($auth->id()!=3)
			$q .= " AND `added_by`!=3";

		$db = VsDatabase::Instance();
		$res = $db->query($q);
		if($ro = mysql_fetch_assoc($res)) {
			$operation = JunOperation::makeFromDb($ro);
		}
		else
			$operation = JunOperation::makeNew();

		return $operation;
	}
	//================================================================================

	static function makeOperatorGroups() {
		$auth = VsAuth::Instance();
		$listGroups = array();
		$q = "SELECT * FROM `".JunOperator::$_sqlTableGroups."`";

		/*
		if($auth->id()!=3)
			$q .= " WHERE `added_by`!=3";

		$q .= " ORDER BY `name`";
		*/

		$db = VsDatabase::Instance();
		$res = $db->query($q);
		while($ro = mysql_fetch_object($res)) {
			/*
			if($operator = JunOperator::makeFromDb($ro))
				$listOperators[] = $operator;
			*/

			array_push($listGroups, $ro);
		}

		return $listGroups;
	}
	//================================================================================

	static function addOperatorGroup($name) {
		$auth = VsAuth::Instance();
		$q = "INSERT INTO `".JunOperator::$_sqlTableGroups."` (`name`) VALUES('".$name."')";

		/*
		if($auth->id()!=3)
			$q .= " WHERE `added_by`!=3";

		$q .= " ORDER BY `name`";
		*/

		$db = VsDatabase::Instance();
		$res = $db->query($q);
		
		if($res)
			return mysql_num_rows($res);
		else
			return FALSE;
	}
	//================================================================================


	static function makeOperators($group=-1) {
		$auth = VsAuth::Instance();
		$listOperators = array();
		$q = "SELECT * FROM `".JunOperator::$_sqlTable."`";
		if($group<0)
			$q .= " WHERE 1";
		else
			$q .= " WHERE groups_id=".$group." AND removed=0";

		if($auth->id()!=3)
			$q .= " AND `added_by`!=3";

		$q .= " ORDER BY `name`";

		$db = VsDatabase::Instance();
		$res = $db->query($q);
		while($ro = mysql_fetch_assoc($res)) {
			if($operator = JunOperator::makeFromDb($ro))
				$listOperators[] = $operator;
		}

		return $listOperators;
	}
	//================================================================================

	static function makeOperator($id) {
		$auth = VsAuth::Instance();
		$q = "SELECT * FROM `".JunOperator::$_sqlTable."` WHERE id=".$id;
		if($auth->id()!=3)
			$q .= " AND `added_by`!=3";

		$db = VsDatabase::Instance();
		$res = $db->query($q);
		if($ro = mysql_fetch_assoc($res)) {
			$operator = JunOperator::makeFromDb($ro);
		}
		else
			$operator = JunOperator::makeNew();

		return $operator;
	}
	//================================================================================

	static function operatorsWork($groupId=-1, $minDate, $maxDate) {
		$grandTotal = 0;
		$q = "SELECT *, SUM(`total`) AS `sub_total` FROM `operators_work_totals`";
		
		if($groupId<0)
			$q .= " WHERE 1";
		else
			$q .= " WHERE `group_id`=".$groupId;

		if($minDate == $maxDate)
			$q .= " AND `date`='".$minDate."'";
		else
			$q .= " AND `date`>='".$minDate."' AND `date`<='".$maxDate."'";

		$q .= " GROUP BY `operators_id`";
		$q .= " ORDER BY `operator_name`";


		$db = VsDatabase::Instance();
		$res = $db->query($q);
		//echo mysql_error();
		$opWork = array();
		while($ro = mysql_fetch_object($res)) {
			$grandTotal += $ro->sub_total;
			$opWork[] = $ro;
		}

		return array($opWork, $grandTotal);
	}
	//================================================================================

	static function makeOperationsWorkCount($modelId, $minDate, $maxDate, $groupOperations=true) {
		$grandTotal = 0;
		$auth = VsAuth::Instance();
		$listOperations = array();

		$q = "SELECT *";
		
		if($groupOperations){
			$q .= ", SUM(count) AS sum_count, SUM(total) AS sum_total";
		}
		
		$q .= " FROM `operators_work_totals` WHERE models_id=".$modelId." AND `date`>='".$minDate."' AND `date`<='".$maxDate."'";
		
		if($groupOperations) {
			$q .= " GROUP BY operations_id";
			$q .= " ORDER BY CAST(operation_id AS UNSIGNED)";		// ORDER BY `operation_id`
		}

		//die($q);
		$db = VsDatabase::Instance();
		$res = $db->query($q);
		//echo mysql_error();

		$operationsTotalCount = array();
		while($ro = mysql_fetch_object($res)) {
			if(!isset($operationsTotalCount[$ro->operations_id])) {
				$operationsTotalCount[$ro->operations_id] = 0;
			}
			
			if($groupOperations) {
				$operationsTotalCount[$ro->operations_id] += $ro->sum_count;
				$grandTotal += $ro->sum_total;
			}
			else {
				$operationsTotalCount[$ro->operations_id] += $ro->count;
				$grandTotal += $ro->total;
			}
			
			
			$listOperations[] = $ro;
		}

		return array($listOperations, $grandTotal, $operationsTotalCount);
	}
	//================================================================================

	static function makeOperatorsPairs($group=-1) {
		$auth = VsAuth::Instance();
		$listOperators = array();
		$q = "SELECT * FROM `".JunOperator::$_sqlTable."`";
		if($group<0)
			$q .= " WHERE 1";
		else
			$q .= " WHERE groups_id=".$group;
	
		$q .= " AND removed=0";
		
		if($auth->id()!=3)
			$q .= " AND `added_by`!=3";
	
		$q .= " ORDER BY `name`";
	
		$db = VsDatabase::Instance();
		$res = $db->query($q);
		while($ro = mysql_fetch_assoc($res)) {
			$listOperators[$ro['id']] = $ro['name'];
		}
	
		return $listOperators;
	}
	//================================================================================
};

?>
